package com.example.blogserver.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blogserver.a_utils.Constant;
import com.example.blogserver.a_utils.ResponseBodyMessage;
import com.example.blogserver.mapper.SensitiveWordMapper;
import com.example.blogserver.mapper.UserMapper;
import com.example.blogserver.pojo.SensitiveWord;
import com.example.blogserver.pojo.User;
import com.example.blogserver.service.SensitiveWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @description
 * @descriptionDetail
 * @Author B3300
 * @copyright 摸鱼王胖嘟嘟
 * @Date 2024/3/28 14:34
 */
@Service
public class SensitiveWordServiceImpl extends ServiceImpl<SensitiveWordMapper, SensitiveWord> implements SensitiveWordService {

    @Autowired
    private SensitiveWordMapper sensitiveWordMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public ResponseBodyMessage<Page> page(int page, int pageSize) {
        // 查询所有用户信息
        Page pageInfo = new Page(page, pageSize);
        ResponseBodyMessage responseBodyMessage = new ResponseBodyMessage();
        LambdaQueryWrapper<SensitiveWord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(SensitiveWord::getId); // 按照用户ID降序排序
        responseBodyMessage.setStatus(1);
        responseBodyMessage.setMessage("获取成功");
        responseBodyMessage.setData(this.page(pageInfo, lambdaQueryWrapper));
        return responseBodyMessage;
    }

    @Override
    public ResponseBodyMessage<String> insertSensitiveWord(HttpServletRequest request, String word) {
        ResponseBodyMessage responseBodyMessage = new ResponseBodyMessage();
        String account = (String) request.getSession(false).getAttribute(Constant.USERINFO_SESSION_KEY);
        User user = userMapper.queryUser(account);
        if (user.getIdentity() == 2) {
            SensitiveWord sensitiveWord = new SensitiveWord();
            sensitiveWord.setWord(word);
            sensitiveWordMapper.insert(sensitiveWord);
            responseBodyMessage.setStatus(1);
            responseBodyMessage.setMessage("添加敏感词成功!");
            responseBodyMessage.setData("添加敏感词成功!");
        } else {
            responseBodyMessage.setStatus(-1);
            responseBodyMessage.setMessage("无权限!");
            responseBodyMessage.setData("无权限!");
        }
        return responseBodyMessage;
    }

    @Override
    public ResponseBodyMessage<String> updateSensitiveWord(HttpServletRequest request, String word,String updateNewWord) {
        ResponseBodyMessage responseBodyMessage = new ResponseBodyMessage();
        String account = (String) request.getSession(false).getAttribute(Constant.USERINFO_SESSION_KEY);
        User user = userMapper.queryUser(account);
        if (user.getIdentity() == 2) {
            SensitiveWord wordByStringWord = sensitiveWordMapper.getWordByStringWord(word);
            wordByStringWord.setWord(updateNewWord);
            sensitiveWordMapper.updateById(wordByStringWord);
            responseBodyMessage.setStatus(1);
            responseBodyMessage.setMessage("修改敏感词成功!");
            responseBodyMessage.setData("修改敏感词成功!");
        } else {
            responseBodyMessage.setStatus(-1);
            responseBodyMessage.setMessage("无权限!");
            responseBodyMessage.setData("无权限!");
        }
        return responseBodyMessage;
    }

    @Override
    public ResponseBodyMessage<String> deleteSensitiveWord(HttpServletRequest request, String word) {
        ResponseBodyMessage responseBodyMessage = new ResponseBodyMessage();
        String account = (String) request.getSession(false).getAttribute(Constant.USERINFO_SESSION_KEY);
        User user = userMapper.queryUser(account);
        if (user.getIdentity() == 2) {
            SensitiveWord wordByStringWord = sensitiveWordMapper.getWordByStringWord(word);
            sensitiveWordMapper.deleteById(wordByStringWord);
            responseBodyMessage.setStatus(1);
            responseBodyMessage.setMessage("删除敏感词成功!");
            responseBodyMessage.setData("删除敏感词成功!");
        } else {
            responseBodyMessage.setStatus(-1);
            responseBodyMessage.setMessage("无权限!");
            responseBodyMessage.setData("无权限!");
        }
        return responseBodyMessage;
    }
}
